<?php

$resourceListStr = Yii::t('site','Resource List');
$creatorStr = Yii::t('site','Creator');
$resourceNameStr = Yii::t('site','Name');
$statusStr = Yii::t('site','Status');
$actionStr = Yii::t('site','Action');
$amountStr = Yii::t('site','Amount');
$addResourceStr = Yii::t('site','Add');
$deleteStr = Yii::t('site','Delete');
$modifyStr = Yii::t('site','Modify');
$prevStr = Yii::t('site','<<');
$nextStr = Yii::t('site','>>');
$activeStr = Yii::t('site','active');

$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$app = Blyn::app()->getActiveApp();

if ($screen->isAjaxLinkClicked($deleteStr)) {
    if ($app instanceof BBookingSports) {
        if (!empty($_POST))
        { 
            $checkedArray = $_POST['checkbox']; 
        } 
        foreach ($checkedArray as $primary_id) { 
            $resourceModel = new BSResourceFormModel();
            $resourceModel->id = $primary_id;
            $app->deleteResource($resourceModel);
        }        
    }
}

$sportTypeList = Blyn::app()->getActiveApp()->getResourceGroupListData(BSResourceGroup::SPORT_TYPE);
$areaList = Blyn::app()->getActiveApp()->getResourceGroupListData(BSResourceGroup::AREA);
$stadiumList = Blyn::app()->getActiveApp()->getResourceGroupListData(BSResourceGroup::STADIUM);

$resources = Blyn::app()->getActiveApp()->getResourceList();
$totalRows = count($resources);
$rowsPerPage = 5;
if (!empty($_POST))
    $pageNo = $_POST['curPageNo'];
else
    $pageNo = 1;
$totalpages = ceil($totalRows/$rowsPerPage);

if ($screen->isAjaxLinkClicked($prevStr))
{
        if ($app instanceof BBookingSports) {
            if (!empty($_POST))
            { 
                $pageNo--;
                if($pageNo<1)
                    $pageNo = 1;               
            }
        }
}

if ($screen->isAjaxLinkClicked($nextStr))
{
        if ($app instanceof BBookingSports) {
            if (!empty($_POST))
            { 
                $pageNo++;
                if($pageNo>$totalpages)
                    $pageNo = $totalpages;               
            }
        }
}
    
//$p2btn = $screen->buildAjaxLink('p2', '2', NULL, 'resourceList', array('update' => "#resourceDataTable", 'type' => 'post'), array('class' => 'btn'));
for($j=0; $j<$totalpages; $j++)
{
    $page_number = strval($j + 1);
    if ($screen->isAjaxLinkClicked($page_number))
    {
        if ($app instanceof BBookingSports) {
            if (!empty($_POST))
            { 
                $pageNo = $page_number;
                break;
            }
        }
    }
}

$startNo = ($pageNo-1)*$rowsPerPage;
if($totalRows < $pageNo*$rowsPerPage)
    $endNo = $totalRows;
else
    $endNo = $startNo + $rowsPerPage;

//$i = 0;
//foreach ($resources as $id => $resource) {
//        if($i<$startNo || $i>$endNo-1)
//                unset($resources[$id]);
//        $i++;
//}
?>

<script type="text/javascript"> 
  $(document).ready(function(){ 
    $("#selectAll").click(function(){ 
      if($(this).attr("checked")=="checked"){ 
        $(":checkbox").attr("checked",true); 
      }else{ 
        $(":checkbox").attr("checked",false); 
      } 
    }); 
    
  }); 
 </script> 

<div><legend><?php echo $resourceListStr?></legend></div>
<?php echo CHtml::beginForm(); ?>
<fieldset>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>
                    <input type="checkbox"  id="selectAll"> 
                </th>
                <th><?php echo $resourceNameStr?></th>
                <th><?php echo $amountStr?></th>
                <th><?php echo $creatorStr?></th>
                <th><?php echo $statusStr?></th>
                <th><?php echo $actionStr?></th>
            </tr> 
        </thead>
        <tbody>
            <?php
            $i = 0;
            foreach ($resources as $resource) {
                 if($i>=$startNo && $i<=$endNo-1)
                 {
                $resourceId = $resource->getId();
                $creatorName = Yii::t('site','no-name');
                $creatorId = $resource->creatorId;
                $amount = $resource->amount;
                if (is_numeric($creatorId) && $creatorId > 0) {
                    $creator = new BUser($creatorId);
                    $creatorName = $creator->getName();
                }
                $statusName = $activeStr;
                if (isset($resource->status)) {
                    $status = new BSStatus($resource->status);
//                    $statusName = $status->getName();
                     $statusName = Yii::t('site',$status->getName());
                }
                $resourceName = $resource->getName();                
                //   $ajaxJoinBtn = $screen->buildAjaxLink('joinSpaceBtn', 'Join to Space', 'leftMenu', NULL, array('update' => "#leftbar", 'type' => 'post'), array('id' => 'joinBtn' . $space->getId(), 'class' => 'btn'));
                //$spaceId = $space->getId();
                //$senderPermits = $screen->getSenderPermits($spaceId);
                //$allowRoles = current($senderPermits);
                ?>
                <tr>
                    <td><label class="checkbox">
                            <input type="checkbox" name="checkbox[]" value=<?php echo $resourceId?>> 
                        </label></td>
                    <td>
                        <?php echo $resourceName; ?>
                    </td>
                    <td>
                        <?php echo $amount; ?>
                    </td>
                    <td>
                        <?php echo $creatorName; ?>
                    </td>
                    <td>
                        <?php echo $statusName; ?>
                    </td>
                    <td>
                        <div class="row-fluid">
                            <a href="#myModal<?php echo $resourceId; ?>" role="button" data-toggle="modal"><?php echo $modifyStr; ?></a>
                        </div>
                    </td>
                </tr>
                <!-- Modal -->
            <div id="myModal<?php echo $resourceId; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <?php $screen->renderView('manageResource',
                        array('resourceId' => $resourceId, 'resource'=>$resources[$resourceId],
                            'sportTypeList'=>$sportTypeList, 'areaList'=>$areaList,
                            'stadiumList'=>$stadiumList)); ?>
            </div>
        <?php }
            $i++;
            }?>
        </tbody>
    </table>
        
    <div class="row-fluid">
        <a href="#myModal0" role="button" data-toggle="modal" class="btn"><?php echo $addResourceStr; ?></a>
        <?php if($totalpages>0){ ?>
        <?php echo $screen->getAjaxLink($deleteStr, array('htmlOptions'=>array('class'=>'btn'))); ?>
        <?php } ?>
    </div>
    <div id="myModal0" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <?php $screen->renderView('manageResource', array('resourceId' => '0',
                'resource'=>null, 'sportTypeList'=>$sportTypeList,
                'areaList'=>$areaList, 'stadiumList'=>$stadiumList)); ?>
    </div>   
    <div><input type="hidden" name="curPageNo" value=<?php echo $pageNo; ?>></div>
    <?php echo $screen->getAjaxLink('Refresh', array('htmlOptions'=>array('id'=>'refreshIt', 'style'=>'display:none'))); ?>
</fieldset>
<body>  
<div class="pagination">  
  <ul>  
    <li <?php
        $disabled = ($pageNo==1 || $totalpages==0);
        if($disabled) echo "class='active'"; ?>>
            <?php 
            if($pageNo + 1>$totalpages)
                $curPageNoStr = strval($totalpages);
            else
                $curPageNoStr = strval($pageNo + 1);
            
            if ($disabled) {
                echo '<a href="javascript:void()">'.$prevStr.'</a>';
            } else {
                echo $screen->getAjaxLink($prevStr, array('data'=>$curPageNoStr));
            }
            ?>
    </li>  
    <?php for($j=0; $j<$totalpages; $j++){ 
        $curPageNoStr = strval($j + 1);
    ?>
    <li <?php
        $disabled = ($pageNo == $j + 1);
        if ($disabled) echo "class='active'"; ?>>
        <?php
            if ($disabled) {
                echo '<a href="javascript:void()">'.$curPageNoStr.'</a>';
            } else {
                echo $screen->getAjaxLink($curPageNoStr);
            }
        ?>
    </li>  
    <?php }
    ?>
    <li <?php
        $disabled = ($pageNo==$totalpages || $totalpages==0);
        if($disabled) echo "class='active'"; ?>>
            <?php 
            if($pageNo<2)
                $curPageNoStr = '1';
            else
                $curPageNoStr = strval($pageNo - 1);
            
            if ($disabled) {
                echo '<a href="javascript:void()">'.$nextStr.'</a>';
            } else {
                echo $screen->getAjaxLink($nextStr, array('data'=>$curPageNoStr));
            }
            ?>
    </li>  
  </ul>  
</div>  
</body>
<?php echo CHtml::endForm(); ?>



